package com.hhkj.api.invoice.service.biz.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hhkj.admin.invoice.domain.InvoiceTitle;
import com.hhkj.admin.invoice.model.dopo.InvoiceTitleDo;
import com.hhkj.admin.invoice.model.dopo.InvoiceTitlePo;
import com.hhkj.admin.invoice.service.base.InvoiceTitleService;
import com.hhkj.api.invoice.model.req.ApiInvoiceTitleReq;
import com.hhkj.api.invoice.model.res.ApiInvoiceTitleRes;
import com.hhkj.api.invoice.service.biz.ApiSaleInvoiceTitleBizService;
import com.hhkj.api.sale.common.session.holder.ApiSaleSessionHolder;
import com.ruoyi.common.cont.SysConst;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ApiSaleInvoiceBizServiceImpl
 *
 * @author zheng
 * @since 2023/11/4 23:07
 */
@Service
public class ApiSaleInvoiceTitleBizServiceImpl implements ApiSaleInvoiceTitleBizService {

    @Autowired
    private InvoiceTitleService titleService;

    @Override
    public boolean removeByIdList(List<Long> idList) {
        // 校验对应的发票是否是当前用户的数据
        List<InvoiceTitle> titlePoList = titleService.listByIds(idList);
        Long saleId = ApiSaleSessionHolder.getSaleId();
        for (InvoiceTitle titlePo : titlePoList) {
            if (!titlePo.getSaleId().equals(saleId)) {
                throw new GlobalException("无权限删除该抬头");
            }
        }
        return titleService.removeBatchByIds(idList);
    }

    @Override
    public ApiInvoiceTitleRes getDefaultInvoiceTitle() {
        InvoiceTitleDo invoiceTitleDo = new InvoiceTitleDo();
        invoiceTitleDo.setSaleId(ApiSaleSessionHolder.getSaleId());
        invoiceTitleDo.setDefaultFlag(SysConst.Flag.IS.getFlag());
        InvoiceTitlePo titlePo = titleService.getPoByDo(invoiceTitleDo);
        if (ObjectUtil.isNull(titlePo)) {
            throw new GlobalException("未配置默认发票抬头");
        }
        return BeanUtil.toBean(titlePo, ApiInvoiceTitleRes.class);
    }

    @Override
    public List<ApiInvoiceTitleRes> list(ApiInvoiceTitleReq req) {
        req.setSaleId(ApiSaleSessionHolder.getSaleId());
        List<InvoiceTitlePo> titlePoList = titleService.listPoByDo(req);
        return BeanUtil.copyToList(titlePoList, ApiInvoiceTitleRes.class);
    }

    @Override
    public boolean save(ApiInvoiceTitleReq req) {
        req.setSaleId(ApiSaleSessionHolder.getSaleId());
        return titleService.save(req);
    }

    @Override
    public boolean update(ApiInvoiceTitleReq req) {
        if (ObjectUtil.isEmpty(req.getId())) {
            throw new GlobalException("id不能为空");
        }
        if (SysConst.Flag.IS.getFlag().equals(req.getDefaultFlag())) {
            titleService.clearDefaultFlag(ApiSaleSessionHolder.getSaleId());
        }
        return titleService.updateById(req);
    }

    @Override
    public InvoiceTitle get(Long titleId) {
        return titleService.getById(titleId);
    }
}
